<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class PeisongOrder extends Base {
    public  $order_status;
    public $peisong_status;
    /*
     * 初始化操作
     */
    public function initialize() {
        parent::initialize();
        $this->order_status = logic('OrderLogic')->getStatus();
        $this->peisong_status = logic('OrderLogic')->getPeisongStatus();
        // 订单 支付 发货状态
        View::assign('order_status',$this->order_status);
    }

    public function index(){
        //$map = $this->_search();
        if(input('?param.status')){
            $status = input('status');
        }else{
            $status = input('status')?:'all';
        }
        $goods_leixing = Db::name('goods_leixing')->where('1=1')->column('name','id');
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(SID){
                $map['o.sid'] = SID;
            }
            if(input('user_id')){
                $map['o.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['o.username'] = input('username');
            }
            if(input('order_sn')){
                $map['o.order_sn'] = input('order_sn');
            }
            if(input('?param.status') && input('status')!='all'){
                $map['po.status'] = input('status');
            }
            if(input('?param.order_status') && input('order_status')!='all'){
                $map['o.order_status'] = input('order_status');
            }
            if(input('ps_id')){
                $map['po.ps_id'] = input('ps_id');
            }


            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['o.add_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('ps_order')->alias('po')->where($map)->count();

            if(input('s_username')){
                $map['s.username|s.name'] = ['like','%'.input('s_username').'%'];
            }
            $orders_status = $this->order_status;
            $peisong_status = $this->peisong_status;
            $field = 'o.*,po.id,po.ps_id,po.ticheng,po.juli,po.status';
            $list = Db::name('ps_order')
                ->alias('po')
                ->join('order o',' po.order_id = o.order_id','left')
                ->field($field)
                ->where($map)
                ->order('po.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['leixing_name'] = $goods_leixing[$v['leixing_id']];
                $v['status_str'] = $orders_status[$v['order_status']];
                $v['add_time'] = $v['add_time']?date('Y-m-d H:i:s',$v['add_time']):'';
                $v['pay_time'] = $v['pay_time']?date('Y-m-d H:i:s',$v['pay_time']):'';
                $v['confirm_time'] = $v['confirm_time']?date('Y-m-d H:i:s',$v['confirm_time']):'';
                $v['shipping_time'] = $v['shipping_time']?date('Y-m-d H:i:s',$v['shipping_time']):'';
                $v['ps_name'] = Db::name('peisong_user')->where('id',$v['ps_id'])->value('realname');
                $v['peisong_status_str'] = $peisong_status[$v['status']];
                $list[$k] = $v;
            }
            $total = Db::name('ps_order')
                ->alias('po')
                ->join('order o',' ps.order_id = o.order_id','left')
                ->where($map)
                ->field('sum(total_shop_price) total_shop_price,sum(total_pv) total_pv')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        $shipping_list = Db::name('shipping')->field('shipping_name,shipping_code')->select();
        View::assign('shipping_list',$shipping_list);
        View::assign("goods_leixing",$goods_leixing);
        View::assign('status',$status);
        View::assign('sid',SID);
        return View::fetch();
    }

    /**
     * 导出订单
     */
    public function excel()
    {
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(SID){
            $map['o.sid'] = SID;
        }
        if(input('user_id')){
            $map['o.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['o.username'] = input('username');
        }
        if(input('order_sn')){
            $map['o.order_sn'] = input('order_sn');
        }
        if(input('?param.status') && input('status')!='all'){
            $map['o.order_status'] = input('status');
        }
        if(input('?param.order_status') && input('order_status')!='all'){
            $map['o.order_status'] = input('order_status');
        }
        if(input('s_username')){
            $map['s.username|s.name'] = ['like','%'.input('s_username').'%'];
        }
        $goods_leixing = Db::name('goods_leixing')->where('1=1')->column('name','id');
        $orders_status = $this->order_status;
        $field = 'o.*,s.name s_name,s.username s_username';
        $list = Db::name('order')
            ->alias('o')
            ->join('suppliers s',' o.sid = s.id','left')
            ->field($field)
            ->where($map)
            ->order('o.order_id desc ')
            ->page($page,$limit)
            ->select();
        $count = Db::name('order')
            ->alias('o')
            ->join('suppliers s',' o.sid = s.id','left')
            ->field($field)
            ->where($map)
            ->count();
        $title = ['订单ID','会员ID','会员账号','商家账号','商家名称','订单编号','订单类型','收货人','总金额','订单状态','配送方式','物流单号','下单时间','付款时间','发货时间','收货时间'];
        $data = [];
        foreach($list as $k=>$v){
            $v['leixing_name'] = $goods_leixing[$v['leixing_id']];
            $v['status_str'] = $orders_status[$v['order_status']];
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $v['pay_time'] = $v['pay_time']?date('Y-m-d H:i:s',$v['pay_time']):'';
            $v['confirm_time'] = $v['confirm_time']?date('Y-m-d H:i:s',$v['confirm_time']):'';
            $v['shipping_time'] = $v['shipping_time']?date('Y-m-d H:i:s',$v['shipping_time']):'';
            $list[$k] = $v;
            $arr = [
                $v['order_id'],$v['user_id'],$v['username'],$v['s_username'],$v['s_name'],$v['order_sn'],$v['leixing_name'],$v['consignee'],
                $v['total_shop_price'],$v['status_str'],$v['shipping_name'],$v['delivery_no'],$v['add_time'],
                $v['pay_time'],$v['shipping_time'],$v['confirm_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        $this->export_excel($title,$data);
    }

    //删除配送订单
    public function delOrder(){
        $ids = input('ids');
        Db::startTrans();
        $ps_orders = Db::name('ps_order')->where('status',0)->where('id','in',$ids)->select()->toArray();
        if(count($ps_orders)==0){
            return json(['code'=>0,'msg'=>'选择的配送单已经开始配送，不可删除']);
        }
        foreach($ps_orders as $ps_order){
            $res = Db::name('ps_order')->where('status',0)->where('id',$ps_order['id'])->delete();
            $res = Db::name('order')->where('order_id',$ps_order['order_id'])->update(['ps_id'=>0,'ps_user_id'=>0]);
        }
        Db::commit();
        return json(['code'=>1,'msg'=>'删除成功']);
    }
}
